<?php
// ADES API : http://api.tvb.com/artiste/search.php?last=%s&limit=30
// OUT API : ades.php?query=xxx

error_reporting(E_ALL);

$api = 'http://api.tvb.com/artiste/search.php?last=%s&limit=%d&format=json';

if(empty($_REQUEST['q'])) exit;

$limit = empty($_REQUEST['limit']) ? 10 : (int)$_REQUEST['limit'];

$api = sprintf($api,$_REQUEST['q'],$limit);

$cp = curl_init($api);
curl_setopt($cp,CURLOPT_RETURNTRANSFER,1);
$rs = curl_exec($cp);

if (curl_errno($cp) > 0) exit;

curl_close($cp);

/*
$hq = new HttpRequest($api);

$rs = json_encode(array());
try{
	$hq->send();
	if($hq->getResponseCode()==200){
		$rs = $hq -> getResponseBody();
	}
}catch(HttpException $e){}
*/
$arr = json_decode($rs,true);


$out = '';
foreach($arr as $row){
	$out .= (empty($row['cname']) ? $row['ename'] : $row['cname']) . '|' . $row['artid'] . "\n";
}

echo $out;
